//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SwapBasisSystem.h"
using namespace Cephei::QL::Models::Marketmodels::Callability;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/Callability/MarketModelBasisSystem.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::CSwapBasisSystem (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ exerciseTimes) : CMarketModelBasisSystem(CSwapBasisSystem::typeid)
{
    try
    {
#ifdef HANDLE
        _phSwapBasisSystem = NULL;
#endif
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        exerciseTimes->Lock();
        INativeVector<Double>^ _NCIexerciseTimes = exerciseTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCexerciseTimes = safe_cast<CDoubleVector^>(_NCIexerciseTimes);
        std::vector<QuantLib::Time>& _exerciseTimes = static_cast<std::vector<QuantLib::Time>&> (_NCexerciseTimes->GetReference ());
        _ppSwapBasisSystem = new boost::shared_ptr<QuantLib::SwapBasisSystem> (new QuantLib::SwapBasisSystem ( _rateTimes,  _exerciseTimes ));
        SetMarketModelBasisSystem (boost::dynamic_pointer_cast<QuantLib::MarketModelBasisSystem> (*_ppSwapBasisSystem));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
        if (exerciseTimes != nullptr) exerciseTimes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::CSwapBasisSystem (boost::shared_ptr<QuantLib::SwapBasisSystem>& childNative, Object^ owner) : CMarketModelBasisSystem(CSwapBasisSystem::typeid)
{
#ifdef HANDLE
	_phSwapBasisSystem = NULL;
#endif
	_ppSwapBasisSystem = &childNative;
    _ppMarketModelBasisSystem = new boost::shared_ptr<QuantLib::MarketModelBasisSystem> (boost::dynamic_pointer_cast<QuantLib::MarketModelBasisSystem> (*_ppSwapBasisSystem));
}
Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::CSwapBasisSystem (QuantLib::SwapBasisSystem& childNative, Object^ owner) : CMarketModelBasisSystem(CSwapBasisSystem::typeid)
{
#ifdef HANDLE
	_phSwapBasisSystem = NULL;
#endif
	_ppSwapBasisSystem = new boost::shared_ptr<QuantLib::SwapBasisSystem> (&childNative);
    _ppMarketModelBasisSystem = new boost::shared_ptr<QuantLib::MarketModelBasisSystem> (boost::dynamic_pointer_cast<QuantLib::MarketModelBasisSystem> (*_ppSwapBasisSystem));
    _SwapBasisSystemOwner = owner;
    _MarketModelBasisSystemOwner = owner;
}

Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::CSwapBasisSystem (CSwapBasisSystem^ copy) : CMarketModelBasisSystem(CSwapBasisSystem::typeid)
{
#ifdef HANDLE
	_phSwapBasisSystem = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwapBasisSystem = new boost::shared_ptr<QuantLib::SwapBasisSystem> (copy->GetShared());
        _ppMarketModelBasisSystem = new boost::shared_ptr<QuantLib::MarketModelBasisSystem> (boost::dynamic_pointer_cast<QuantLib::MarketModelBasisSystem> (*_ppSwapBasisSystem));
    }
}
Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::CSwapBasisSystem (PLATFORM::Type^ t) : CMarketModelBasisSystem(CSwapBasisSystem::typeid)
{
#ifdef HANDLE
	_phSwapBasisSystem = NULL;
#endif
	if (!t->IsSubclassOf(CSwapBasisSystem::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::CSwapBasisSystem (QuantLib::Handle<QuantLib::SwapBasisSystem>& childNative, Object^ owner)  : CMarketModelBasisSystem(CSwapBasisSystem::typeid)
{
	_phSwapBasisSystem = &childNative;
	_ppSwapBasisSystem = &static_cast<boost::shared_ptr<QuantLib::SwapBasisSystem>>(childNative.currentLink());
    _ppMarketModelBasisSystem = new boost::shared_ptr<QuantLib::MarketModelBasisSystem> (boost::dynamic_pointer_cast<QuantLib::MarketModelBasisSystem> (*_ppSwapBasisSystem));
    _SwapBasisSystemOwner = owner;
}
Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::CSwapBasisSystem (QuantLib::Handle<QuantLib::SwapBasisSystem> childNative)  : CMarketModelBasisSystem(CSwapBasisSystem::typeid)
{
	_phSwapBasisSystem = &childNative;
	_ppSwapBasisSystem = &static_cast<boost::shared_ptr<QuantLib::SwapBasisSystem>>(childNative.currentLink());
    _ppMarketModelBasisSystem = new boost::shared_ptr<QuantLib::MarketModelBasisSystem> (boost::dynamic_pointer_cast<QuantLib::MarketModelBasisSystem> (*_ppSwapBasisSystem));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::CSwapBasisSystem (QuantLib::SwapBasisSystem childNative)  : CMarketModelBasisSystem(CSwapBasisSystem::typeid)
{
#ifdef HANDLE
	_phSwapBasisSystem = NULL;
#endif
	_ppSwapBasisSystem = new boost::shared_ptr<QuantLib::SwapBasisSystem> (new QuantLib::SwapBasisSystem (childNative));
    _ppMarketModelBasisSystem = new boost::shared_ptr<QuantLib::MarketModelBasisSystem> (boost::dynamic_pointer_cast<QuantLib::MarketModelBasisSystem> (*_ppSwapBasisSystem));
}
#endif

Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::~CSwapBasisSystem ()
{
    if (_ppSwapBasisSystem != NULL)
    {
	    delete _ppSwapBasisSystem;
        _ppSwapBasisSystem = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::!CSwapBasisSystem ()
{
    if (_ppSwapBasisSystem != NULL)
    {
	    delete _ppSwapBasisSystem;
    }
}
QuantLib::SwapBasisSystem& Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::GetReference ()
{
    if (_ppSwapBasisSystem == NULL) throw REFNEW NativeNullException ();
	return **_ppSwapBasisSystem;
}
boost::shared_ptr<QuantLib::SwapBasisSystem>& Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::GetShared ()
{
    if (_ppSwapBasisSystem == NULL) throw REFNEW NativeNullException ();
	return *_ppSwapBasisSystem;
}
QuantLib::SwapBasisSystem* Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::GetPointer ()
{
    if (_ppSwapBasisSystem == NULL) throw REFNEW NativeNullException ();
	return &**_ppSwapBasisSystem;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwapBasisSystem>& Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::GetHandle ()
{
	if (_phSwapBasisSystem == NULL)
	{
		_phSwapBasisSystem = new Handle<QuantLib::SwapBasisSystem> (*_ppSwapBasisSystem);
	}
	return *_phSwapBasisSystem;
}
#endif
bool Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::HasNative () 
{
	return (_ppSwapBasisSystem != NULL);
}

Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppSwapBasisSystem)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::ISwapBasisSystem^ Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::NextStep (Cephei::QL::Models::Marketmodels::ICurveState^ prm1)
{
    CCurveState^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CCurveState^> (prm1);
        _Cprm1->Lock();
        QuantLib::CurveState& _prm1 = static_cast<QuantLib::CurveState&> (_Cprm1->GetReference ()); 
    	(*_ppSwapBasisSystem)->nextStep ( _prm1 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::NumberOfExercises::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSwapBasisSystem)->numberOfExercises ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::NumberOfFunctions::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppSwapBasisSystem)->numberOfFunctions ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::ISwapBasisSystem^ Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppSwapBasisSystem)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::ISwapBasisSystem^ Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem::Values (Cephei::QL::Models::Marketmodels::ICurveState^ currentState, Cephei::Core::IVector<Double>^ results)
{
    CCurveState^ _CcurrentState;
    try
    {
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
        results->Lock();
        INativeVector<Double>^ _NCIresults = results->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCresults = safe_cast<CDoubleVector^>(_NCIresults);
        std::vector<QuantLib::Real>& _results = static_cast<std::vector<QuantLib::Real>&> (_NCresults->GetReference ());
    	(*_ppSwapBasisSystem)->values ( _currentState,  _results );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
        if (results != nullptr) results->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Callability::ISwapBasisSystem^ Cephei::QL::Models::Marketmodels::Callability::CSwapBasisSystem_Factory::Create (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ exerciseTimes)
{
    return REFNEW CSwapBasisSystem ( rateTimes,  exerciseTimes);
}
